package com.xiaoyu.lanling.feature.setting.activity

import `in`.srain.cube.util.CheckUtil
import android.os.Build
import android.os.Bundle
import android.view.View
import com.xiaoyu.base.utils.MMKVUtil
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.data.NotificationData
import com.xiaoyu.lanling.router.Router
import kotlinx.android.synthetic.main.setting_notification_activity.*

class NotificationActivity : AppCompatToolbarActivity() {

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.setting_notification_activity)
        initToolbar()

        initView()
        initBind()
        initData()
    }

    override fun onStartSafelyAfterAppFinishInit(isFirstTimeStart: Boolean) {
        super.onStartSafelyAfterAppFinishInit(isFirstTimeStart)
        updateNotificationStatus()
    }

    private fun updateNotificationStatus() {
        val notificationEnable: Boolean = CheckUtil.isNotificationChannelEnabled(this, NotificationData.GroupId.DEFAULT, NotificationData.ChannelId.DEFAULT_MESSAGE)
        notification_disable_tip.visibility = if (notificationEnable) View.GONE else View.VISIBLE
    }

    private fun initView() {
        title = getString(R.string.setting_notification_title)
    }

    private fun initBind() {
        notification_settings_layout.setOnClickListener { Router.instance.gotoApplicationDetailsSettings(this) }
        notification_service_switch.setOnCheckedChangeListener { _, isChecked -> MMKVUtil.get()?.putBoolean(Constant.MMKVKey.KEY_NOTIFICATION_SERVICE_ENABLE, isChecked) }
    }

    private fun initData() {
        val aboveAndroidO = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
        notification_service_guide_title.visibility = if (aboveAndroidO) View.VISIBLE else View.GONE
        notification_service_layout.visibility = if (aboveAndroidO) View.VISIBLE else View.GONE

        val enable = MMKVUtil.get()?.getBoolean(Constant.MMKVKey.KEY_NOTIFICATION_SERVICE_ENABLE, false)
                ?: false
        notification_service_switch.isChecked = enable
    }
}